package net.wofly.right.web;

import net.wofly.common.persistence.PageWrapper;
import net.wofly.right.domain.FuncList;
import net.wofly.right.domain.RightConfig;
import net.wofly.right.domain.Role;
import net.wofly.right.domain.User;
import net.wofly.right.validator.RoleValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import net.wofly.common.util.ReturnBean;
import net.wofly.right.service.IRoleService;
import net.wofly.right.validator.UserValidator;

import java.util.List;

/**
 * @author siqingwei
 */
@Controller
@RequestMapping(value = "/api/right/role")
@Api(description = "角色管理")
public class RoleController {

    private static final String DEFAULT_PAGE_NUM = "1";
    private static final String DEFAULT_PAGE_SIZE = "10";

    @Autowired
	IRoleService iRoleService ;
	
	@ApiOperation(value = "获取角色列表")
    @RequestMapping(value = "/getAllRole", method = {RequestMethod.GET})
    @ResponseBody
    public ReturnBean<List<Role>> getAllRole() {
        ReturnBean<List<Role>> returnBean = iRoleService.getAllRole();
        return returnBean;
    }
	
	@ApiOperation(value = "保存角色")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "roleName", value = "角色名称", required = true, dataType ="String", paramType = "form"),
        @ApiImplicitParam(name = "warehouseFlag", value = "是否有库存", required = true, dataType ="Integer", paramType = "form"),
        @ApiImplicitParam(name = "warehouseType", value = "库存类型", required = true,dataType = "Integer", paramType = "form")})
    @RequestMapping(value = "/save", method = {RequestMethod.POST})
    @ResponseBody
    public ReturnBean<Role> save(String roleID, String roleName, Integer warehouseFlag ,Integer warehouseType) {
        ReturnBean<Role> returnBean;
        String errorMessage = RoleValidator.validateSaveParams(roleName, warehouseFlag , warehouseType);
        if (errorMessage.length() > 0) {
            returnBean = new ReturnBean<>(-1, errorMessage);
        } else {
            returnBean = iRoleService.saveRole(roleID, roleName, warehouseFlag , warehouseType);
        }
        return returnBean;
    }
	
	@ApiOperation(value = "获取角色下人员列表")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "roleID", value = "角色ID", required = true, dataType = "String",paramType = "query"),
        @ApiImplicitParam(name = "pageNum", value ="页码（默认第一页）", required = true, dataType = "int", paramType = "query"),
        @ApiImplicitParam(name = "pageSize", value = "每页显示条数（默认10条）", required = true, dataType = "int", paramType = "query")
    })
    @RequestMapping(value = "/getUserByRoleID", method = {RequestMethod.GET})
    @ResponseBody
    public ReturnBean<PageWrapper<User>> getUserByRoleID(String roleID,
                                                         @RequestParam(defaultValue = DEFAULT_PAGE_NUM) Integer pageNum,
                                                         @RequestParam(defaultValue = DEFAULT_PAGE_SIZE) Integer pageSize) {
        ReturnBean<PageWrapper<User>> returnBean;
        String errorMessage = RoleValidator.validateGetUserByRoleIDParams(roleID);
        if (errorMessage.length() > 0) {
            returnBean = new ReturnBean<>(-1, errorMessage);
        } else {
            returnBean = iRoleService.getUserByRoleID(roleID, pageNum, pageSize);
        }
        return returnBean;
    }

    @ApiOperation(value = "根据角色ID获取接口功能列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleID", value = "角色ID", required = true, dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "pageNum", value ="页码（默认第一页）", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示条数（默认10条）", required = true, dataType = "int", paramType = "query")
    })
    @RequestMapping(value = "/getFuncListByRoleIDForInterface", method = {RequestMethod.GET})
    @ResponseBody
    public ReturnBean<PageWrapper<FuncList>> getFuncListByRoleIDForInterface(String roleID,
                                                             @RequestParam(defaultValue = DEFAULT_PAGE_NUM) Integer pageNum,
                                                             @RequestParam(defaultValue = DEFAULT_PAGE_SIZE) Integer pageSize) {
        ReturnBean<PageWrapper<FuncList>> returnBean;
        String errorMessage = RoleValidator.validateGetUserByRoleIDParams(roleID);
        if (errorMessage.length() > 0) {
            returnBean = new ReturnBean<>(-1, errorMessage);
        } else {
            returnBean = iRoleService.getFuncListByRoleID(roleID, 1, pageNum, pageSize);
        }
        return returnBean;

    }

    @ApiOperation(value = "根据角色ID获取菜单功能列表")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "roleID", value = "角色ID", required = true, dataType = "String",paramType = "query"),
        @ApiImplicitParam(name = "pageNum", value ="页码（默认第一页）", required = true, dataType = "int", paramType = "query"),
        @ApiImplicitParam(name = "pageSize", value = "每页显示条数（默认10条）", required = true, dataType = "int", paramType = "query")
    })
    @RequestMapping(value = "/getFuncListByRoleIDForMenu", method = {RequestMethod.GET})
    @ResponseBody
    public ReturnBean<PageWrapper<FuncList>> getFuncListByRoleIDForMenu(String roleID,
                                                                             @RequestParam(defaultValue = DEFAULT_PAGE_NUM) Integer pageNum,
                                                                             @RequestParam(defaultValue = DEFAULT_PAGE_SIZE) Integer pageSize) {
        ReturnBean<PageWrapper<FuncList>> returnBean;
        String errorMessage = RoleValidator.validateGetUserByRoleIDParams(roleID);
        if (errorMessage.length() > 0) {
            returnBean = new ReturnBean<>(-1, errorMessage);
        } else {
            returnBean = iRoleService.getFuncListByRoleID(roleID, 2, pageNum, pageSize);
        }
        return returnBean;
    }

    @ApiOperation(value = "保存角色与功能关系")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleName", value = "角色名称", required = true, dataType ="String", paramType = "form"),
            @ApiImplicitParam(name = "funcListID", value = "功能ID", required = true, dataType ="String", paramType = "form")
    })
    @RequestMapping(value = "/addRightConfig", method = {RequestMethod.POST})
    @ResponseBody
    public ReturnBean<RightConfig> addRightConfig(String roleID, String funcListID) {
        ReturnBean<RightConfig> returnBean;
        String errorMessage = RoleValidator.validateAddRightConfigParams(roleID, funcListID);
        if (errorMessage.length() > 0) {
            returnBean = new ReturnBean<>(-1, errorMessage);
        } else {
            returnBean = iRoleService.addRightConfig(roleID, funcListID);
        }
        return returnBean;
    }
    
    @ApiOperation(value = "批量保存角色与功能关系")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "rightConfigs", value = "角色功能列表", required = true, dataType ="List<RightConfig>", paramType = "body")
    })
    @RequestMapping(value = "/addRightConfigList", method = {RequestMethod.POST})
    @ResponseBody
    public ReturnBean<String> addRightConfigList(@RequestBody List<RightConfig> rightConfigs)  {
        ReturnBean<String> returnBean;
        String errorMessage = RoleValidator.validateAddRightConfigListParams(rightConfigs);
        if (errorMessage.length() > 0) {
            returnBean = new ReturnBean<>(-1, errorMessage);
        } else {
            returnBean = iRoleService.addRightConfigList(rightConfigs);
        }
        return returnBean;
    }

    @ApiOperation(value = "删除角色与功能关系")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleName", value = "角色名称", required = true, dataType ="String", paramType = "form"),
            @ApiImplicitParam(name = "funcListID", value = "功能ID", required = true, dataType ="String", paramType = "form")
    })
    @RequestMapping(value = "/removeRightConfig", method = {RequestMethod.POST})
    @ResponseBody
    public ReturnBean<RightConfig> removeRightConfig(String roleID, String funcListID) {
        ReturnBean<RightConfig> returnBean;
        String errorMessage = RoleValidator.validateRemoveRightConfigParams(roleID, funcListID);
        if (errorMessage.length() > 0) {
            returnBean = new ReturnBean<>(-1, errorMessage);
        } else {
            returnBean = iRoleService.removeRightConfig(roleID, funcListID);
        }
        return returnBean;
    }
    
    @ApiOperation(value = "批量删除角色与功能关系")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "rightConfigIDs", value = "角色功能ID列表", required = true, dataType ="List<String>", paramType = "body")
    })
    @RequestMapping(value = "/removeRightConfigList", method = {RequestMethod.POST})
    @ResponseBody
    public ReturnBean<String> removeRightConfigList(@RequestBody List<String> rightConfigIDs) {
        ReturnBean<String> returnBean;
        String errorMessage = RoleValidator.validateRemoveRightConfigListParams(rightConfigIDs);
        if (errorMessage.length() > 0) {
            returnBean = new ReturnBean<>(-1, errorMessage);
        } else {
            returnBean = iRoleService.removeRightConfigList(rightConfigIDs);
        }
        return returnBean;
    }
}
